package com.jyhz.admin.controller;

import com.jyhz.admin.service.MemberServiceImpl;
import com.jyhz.admin.verifyCode.VerifyCode;
import com.jyhz.common.dto.HttpResult;
import com.jyhz.common.util.HttpStatusCode;
import com.jyhz.common.util.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author:谢君臣
 * @Date:2021/7/9-9:29
 * @version:1.0
 * @function:
 */

@Controller
@Api(tags = "系统控制器")
@Slf4j
public class VerifyCodeController {
    @Autowired
    private MemberServiceImpl memberServiceImpl;

    @Autowired
    RedisTemplate redisTemplate;

    public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";

    @ApiOperation("获取短信验证码")
    @RequestMapping(value = "/getAuthCode", method = RequestMethod.GET)
    @ResponseBody
    public HttpResult getAuthCode(@RequestParam String telephone) {
        log.info("===>>>登录手机号为："+telephone);
        Boolean bool = memberServiceImpl.generateCode(telephone);
        if (bool){
            HttpStatusCode httpStatusCode = HttpStatusCode.OK;
            HttpResult result = new HttpResult(httpStatusCode.getCode(),
                    httpStatusCode.getZhMessage(),
                    null,
                    TimeUtils.nowStr(TimeUtils.DEF_FMT_DATE_TIME));
            return result;
        }
        else{
            HttpStatusCode httpStatusCode = HttpStatusCode.GONE;
            HttpResult result = new HttpResult(httpStatusCode.getCode(),
                    "验证码生成失败！",
                    null,
                    TimeUtils.nowStr(TimeUtils.DEF_FMT_DATE_TIME));
            return result;
        }
    }

    @ApiOperation("获取验证码")
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest request, HttpServletResponse response){
        VerifyCode verifyCode = new VerifyCode();
        BufferedImage image = verifyCode.getImage();
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);

            // 将明文放到 redis
            String randomString = UUID.randomUUID().toString().substring(0,8);
            // 将验证码对象放在缓存中
            redisTemplate.opsForValue().set(randomString,verifyCode.getText(),100, TimeUnit.SECONDS);
            //前端存放session
            HttpSession session = request.getSession();
            session.removeAttribute(SESSION_KEY);
            session.setAttribute(SESSION_KEY, randomString);
            Cookie cookie = new Cookie(SESSION_KEY,randomString);
            response.addCookie(cookie);
            VerifyCode.output(image,response.getOutputStream());
            log.info("===>>>刷新验证码成功 ");
        } catch (Exception e) {
            log.info("===>>>获取验证码失败");
        }
    }
}
